package com.base.tree;

import java.util.List;

public class TreeRender {
	
	private Tree tree = null;
	
	public TreeRender(Tree tree) {
		this.tree = tree;
	}
	
	public StringBuffer initView() {
		StringBuffer html = new StringBuffer();
		//String nodeHtml =  this.doView(null).toString();
		//html.append(nodeHtml);
		// 初始化脚本
		html.append("<script type=\"text/javascript\">");
		html.append("var tree = new Tree(" + "'" + tree.getId() + "'," + "'" +  tree.getContainerId()  + "'" + ");");
		html.append("tree.init();");
		html.append("tree.viewChildNodes('', false);");
		html.append("</script>");
		return html;
	}
	
	public StringBuffer doView(String pid) {
		StringBuffer html = new StringBuffer();
		List<TreeNode> nodes = this.tree.getNodes(pid);
		
		if(pid == null || "".equals(pid)) {
			pid = Tree.rootNodeId;
		}
		
		this.tree.addChildNodes(pid, nodes);
		
		for(TreeNode node : nodes) {
			node.setEventMgr(this.tree.getEventMgr());
			html.append(node.view());
		}
		return html;
	}
	
	public Tree getTree() {
		return tree;
	}

	public void setTree(Tree tree) {
		this.tree = tree;
	}
}
